package com.cloud.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloud.mapper.PhoneRelationMapper;
import com.cloud.mapper.RecordMapper;
import com.cloud.mapper.ext.PhoneRecordMapper;
import com.cloud.params.PhoneParams;
import com.cloud.params.RecordResult;
import com.cloud.pojo.Page;
import com.cloud.pojo.PhoneRelation;
import com.cloud.pojo.PhoneRelationExample;
import com.cloud.pojo.RecordExample;
import com.cloud.pojo.PhoneRelationExample.Criteria;
import com.cloud.pojo.Record;
import com.cloud.result.DataResult;
import com.cloud.service.PhoneService;

/**
 *@Author: liu
 *@Date: Created on 2017年10月24日 下午2:26:29
 */
@Service
public class PhoneServiceImpl implements PhoneService {
	
	@Autowired
	private PhoneRelationMapper phoneMapper;
	@Autowired
	private RecordMapper recordMaper;
	@Autowired
	private PhoneRecordMapper phoneRecordMapper;

	@Override
	public DataResult<PhoneRelation> selectPhoneList(Integer page, Integer rows, String username) {
		// TODO Auto-generated method stub
		PhoneRelationExample example = new PhoneRelationExample();
		int startPos = (page-1)*rows;
		example.setPage(new Page(startPos, rows));
		Criteria criteria = example.createCriteria();
		criteria.andStatusEqualTo(0);//未拨打过的电话
		criteria.andJobNumEqualTo(username);
		int total = phoneMapper.countByExample(example);
		List<PhoneRelation> adminList = phoneMapper.selectByExamplePage(example);
		
		DataResult<PhoneRelation> dataResult = new DataResult<>(total, adminList, null);
		return dataResult;
	}

	@Override
	public PhoneRelation selectById(Integer id) {
		// TODO Auto-generated method stub
		return phoneMapper.selectByPrimaryKey(id);
	}

	@Override
	public Integer updateRemark(PhoneRelation phoneRelation) {
		// TODO Auto-generated method stub
		phoneRelation.setUpdateTime(new Date());
		int i = phoneMapper.updateByPrimaryKeySelective(phoneRelation);
		if(i > 0){
			return i;
		}
		return 0;
	}

	@Override
	public Integer updateRecordFile(PhoneParams phoneParams) {
		// TODO Auto-generated method stub
		int i = 1;
		//根据电话获取用户id
		PhoneRelationExample example = new PhoneRelationExample();
		Criteria criteria = example.createCriteria();
		criteria.andPhoneEqualTo(phoneParams.getPhone());
		List<PhoneRelation> list = phoneMapper.selectByExample(example);
		if(list == null || list.size() == 0){
			return 0;
		}
		/*PhoneRelation phoneRelation = new PhoneRelation();
		phoneRelation.setPhone(phoneParams.getPhone());
		phoneRelation.setRecordFile(phoneParams.getRecordFile());
		int i = phoneMapper.updateByExampleSelective(phoneRelation, example);*/
		//保存录音记录
		//1.先查询该录音文件是否存在
		RecordExample recordExample = new RecordExample();
		com.cloud.pojo.RecordExample.Criteria recordCriteria = recordExample.createCriteria();
		recordCriteria.andRecordFileEqualTo(phoneParams.getRecordFile());
		List<Record> list2 = recordMaper.selectByExample(recordExample);
		if(list2 == null || list2.size() == 0){
			//插入数据
			Record record = new Record();
			record.setDevid(phoneParams.getDevid());
			record.setPhoneRelationId(list.get(0).getId());
			record.setRecordStartTime(new Date());
			record.setRecordFile(phoneParams.getRecordFile());
			recordMaper.insertSelective(record);
			return i;
		}
		//更新数据
		Record record = new Record();
		record.setRecordEndTime(new Date());
		int j = recordMaper.updateByExampleSelective(record, recordExample);
		if(j > 0){
			return i;
		}
		return -1;
		
	}

	@Override
	public DataResult<RecordResult> selectRecordList(Integer page, Integer rows, PhoneParams params) {
		// TODO Auto-generated method stub
		int startPos = (page-1)*rows;
		params.setPage(startPos);
		params.setPageSize(rows);
		//设置查询时间条件
		if(params.getStartTime().equals("undefined")){
			params.setStartTime(null);
			params.setEndTime(null);
		}else {
			params.setStartTime(params.getStartTime()+" 00:00:00");
			params.setEndTime(params.getEndTime()+" 23:59:59");
		}
		if(StringUtils.isNotBlank(params.getPhone())){
			params.setPhone("%"+params.getPhone()+"%");
		}
		//时间条件
		int total = phoneRecordMapper.countByExample(params);
		List<RecordResult> list = phoneRecordMapper.selectRecordListByExample(params);
		DataResult<RecordResult> dataResult = new DataResult<>(total, list, null);
		return dataResult;
	}

}
